import moment from 'moment';

export const dateFormathasTime = (dataStr: string) => {
    const reg = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
    return dataStr.replace(reg, '$1-$2-$3 $4:$5:$6');
};

export const dateFormat = (dataStr: string | number, pattern = 'YYYY-MM-DD HH:mm:ss') => {
    return moment(dataStr).format(pattern);
};

export const dateFormatNow = (dataStr: string | number) => {
    return moment(dataStr).fromNow();
};

export const nowDateStr = (pattern = 'YYYY-MM-DD') => {
    return moment(new Date()).format(pattern);
};

export const getDateTime = () => {
    return nowDateStr('YYYY-MM-DD HH:mm:ss');
};

export const timestampNow = () => {
    return new Date().getTime();
};

//判断日期大小 add by hyw 2021-03-16
export const isBefore = (startDate: string, endDate: string) => {
    return moment(startDate).isBefore(endDate, 'day');
};
//获取几天前日期
export const getDay = (day: number) => {
    const today = new Date();
    const seconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(seconds);

    let tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();

    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

const doHandleMonth = (month: number) => {
    let result:any = month;
    if (month.toString().length == 1) {
        result = "0" + month;
    }
    return result;
}
